package com.sh.rbac.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sh.rbac.core.common.annotation.SysLog;
import com.sh.rbac.core.common.enums.OperationTypeEnum;
import com.sh.rbac.core.common.result.Result;
import com.sh.rbac.domain.dto.loginLog.LoginLogQueryDTO;
import com.sh.rbac.domain.entity.LoginLog;
import com.sh.rbac.domain.vo.loginLog.LoginLogVO;
import com.sh.rbac.service.LoginLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 登录日志控制器
 *
 * @author wb
 * @since 2024-07-01
 */

@Tag(name = "登录日志", description = "登录日志相关登录")
@RestController
@RequestMapping("sys/login-log")
public class LoginLogController {

    @Autowired
    private LoginLogService loginLogService;

    @Operation(summary = "分页查询登录日志列表", description = "根据登录日志条件查询参数进行查询")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = LoginLog.class)))
    @PostMapping("page/{current}/{limit}")
    @PreAuthorize("@ps.hasPerm('sys:login-log:page')")
    public Result<Page<LoginLogVO>> getPage(
            @Parameter(description = "当前页码", required = true, example = "1") @PathVariable Long current,
            @Parameter(description = "每页记录数", required = true, example = "10") @PathVariable Long limit,
            @RequestBody LoginLogQueryDTO loginLogQueryDTO) {
        return Result.success(loginLogService.getPage(current, limit, loginLogQueryDTO));
    }


    @SysLog(module = "系统日志->登录日志管理", type = OperationTypeEnum.BATCH_DELETE)
    @Operation(summary = "批量删除登录日志", description = "批量删除登录日志信息")
    @ApiResponse(responseCode = "200", description = "成功登录日志菜单")
    @DeleteMapping("batch-delete")
    @PreAuthorize("@ps.hasPerm('sys:login-log:batch-delete')")
    public Result<Boolean> batchDelete(@RequestBody List<Long> ids) {
        return loginLogService.removeBatchByIds(ids) ? Result.success() : Result.failure();
    }

    @SysLog(module = "系统日志->登录日志管理", type = OperationTypeEnum.EXPORT)
    @Operation(summary = "导出登录日志", description = "导出登录日志信息")
    @ApiResponse(responseCode = "200", description = "成功导出登录日志信息")
    @GetMapping("export-data/{startCount}/{endCount}")
    @PreAuthorize("@ps.hasPerm('sys:login-log:export')")
    public void exportData(@PathVariable Integer startCount, @PathVariable Integer endCount, HttpServletResponse response) {
        loginLogService.exportData(startCount, endCount, response);
    }
}
